//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var zm = require('../../utils/zm_tools.js')
const app = getApp()

Page({
  data: {
    userInfo: {},
    logged: false,
    takeSession: false,
    //任务标题
    title: '',

    //标签类型
    lxval: "",
    lxlist: [],
    lxindex: 0,

    //赏金
    shangjin: "",
    //截止日期 
    riqi: "2018-12-12",
    //一口价
    yikoujia: "",
    //摘要
    zhaiyao: '',
    //任务图片
    files: [],
    imgs: {},
    //关键信息
    keys: '',
    //位置
    weizhi: {
      latitude: "",
      longitude: "",
      name: "",
      fullname: ""
    },
    //电话
    dianhua:"",
    //置顶
    zhiding:"",
    zdtsval:"1",
    zdtslist:["1","2","3","4","5","6","7"],
    zdtsindex:0,




    requestResult: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    zm.getAddress({
      cbk: function (r) {
        that.setData({
          weizhi: r
        })
      }
    })
    // 获取任务分类列表
    if (app.globalData.renwuleixing.length == 0) {
      //加载类型
      zm.post("/getziyuan/leixing", {}, function (r) {
        app.globalData.renwuleixing = r.jg;
        that.setData({
          lxlist: app.globalData.renwuleixing
        })
      });
    } else {
      this.setData({
        lxlist: app.globalData.renwuleixing
      })
    }
  },
  valuechangge: function (e) {
    let datafild = e.currentTarget.dataset.bind;
    let value = e.detail.value;
    var jso = {};
    jso[datafild] = value;

    this.setData(jso)
  },
  bianqiangaibian: function (e) {
    this.setData({
      lxindex: e.detail.value,
      lxval: this.data.lxlist[e.detail.value].bm
    })
  },
  chooseImage: function (e) {
    var that = this;
    zm.xzImg(1, function (r) {
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      //that.data.files.concat(res.tempFilePaths)追加数组
      that.setData({
        files: r.tempFilePaths
      });
    });
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  xuanzeweizhi: function (e) {
    var that = this;
    zm.getAddress({
      type: 1,
      cbk: function (r) {
        that.setData({
          weizhi: r
        })
      }
    })
  },
  //获取手机号
  getPhoneNumber:function(r){
    console.log(r);
    zm.post("/renwu/add", that.data, function (e) {

    });

  },
  FaBuRenWu: function (e) {
    zm.alertwait("提交中。。。");
    //校验数据
    if (!this.data.title) {
      zm.alert("标题必须填写"); return;
    }
    if (this.data.files.length < 1) {
      zm.alert("请上传图片"); return;
    }
    if (!this.data.lxval) {
      this.data.lxval = this.data.lxlist[this.data.lxindex].bm;
    }
    var that = this;
    //通过校验后，保存暂存的图片
    zm.upfile(this.data.files[0], function (r) {
      console.log('上传图片成功', r);
      //绑定返回的信息
      that.data.img = r;
      zm.post("/renwu/add", that.data, function (e) {
        if (e.isok) {
          zm.alertok("发布成功！");
          // wx.navigateTo({
          //   url: 'pages/logs/logs?user_id=111'　　// 页面 A
          // })
          wx.switchTab({
            url: '../logs/logs'
          })
        }
      });
    });
  }
})
